package kr.moiba.mypage.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kr.moiba.board.service.BoardSvc;
import kr.moiba.user.domain.User;
import kr.moiba.user.service.UserSvc;
import kr.moiba.vertification.service.VertificationSvc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 이 영호(Youngho Lee)
 */

@Controller
@RequestMapping("/mypage")
public class MypageCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private VertificationSvc vertificationSvc;
	
	@Autowired
	private BoardSvc boardSvc;
	
	@Autowired
	private UserSvc userSvc;
	
	@RequestMapping(value={"/verification.do"})
	public Map<String, Object> verification(HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		String userId = (String)session.getAttribute("userId");
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//검증 결과
		condition.put("viewer", userId);

		//페이징 처리 parameter		
		int maxnumVertifi = ServletRequestUtils.getIntParameter(request, "maxnumVertifi", 5);
		int pageVertifi = ServletRequestUtils.getIntParameter(request, "pageVertifi", 1);
		condition.put("maxnum", maxnumVertifi);
		condition.put("page", (pageVertifi-1)*maxnumVertifi);
		
		resultMap.putAll(vertificationSvc.findbyListPaging(condition));
		resultMap.put("maxnumVertifi", maxnumVertifi);
		resultMap.put("pageVertifi", pageVertifi);
		
		//문의글
		condition.put("userId", userId);
		condition.put("type", 2);
		String searchType = ServletRequestUtils.getStringParameter(request, "searchType", "");
		String searchWord = ServletRequestUtils.getStringParameter(request, "searchWord", "");
		
		if(searchType.equals("title")){
			condition.put("title", searchWord);	
		}else if(searchType.equals("cont")){
			condition.put("cont", searchWord);
		}else if(searchType.equals("name")){
			condition.put("userNm", searchWord);
		}
		
		//페이징 처리 parameter
		int maxnumBoard = ServletRequestUtils.getIntParameter(request, "maxnumBoard", 5);
		int pageBoard = ServletRequestUtils.getIntParameter(request, "pageBoard", 1);
		condition.put("maxnum", maxnumBoard);
		condition.put("page", (pageBoard-1)*maxnumBoard);
		
		resultMap.putAll(boardSvc.findbyListPaging(condition));
		resultMap.put("maxnumBoard", maxnumBoard);
		resultMap.put("pageBoard", pageBoard);

		return resultMap;
	}

	@RequestMapping(value={"/userInfo.do"})
	public Map<String, Object> userInfo(HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Integer idx = (Integer)session.getAttribute("userIdx");
		resultMap.put("user", userSvc.findby(idx));

		return resultMap;
	}
	
	@RequestMapping(value={"/updateUser.do"})
//	public String updateUser(HttpServletRequest request, HttpSession session, User user) throws Exception {
	public @ResponseBody Map<String, Object> updateUser(HttpServletRequest request, HttpSession session, User user) throws Exception {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try{
			Integer idx = (Integer)session.getAttribute("userIdx");
			user.setIdx(idx);		
			userSvc.modify(user);
			
			resultMap.put("SUCCESS", true);
			
		}catch(Exception e){
			
			resultMap.put("SUCCESS", false);
			
		}
		
		return resultMap;
		

		//return "redirect:/mypage/userInfo.do";
	}

}